home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 2 / Meeting Pearls Vol. II (1995)(GTI - Schatztruhe)[!].iso / Pearls / dev / GUI / EAGUI / example.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  12KB  |  475 lines

  1. /*
  2.  * $RCSfile: example.c,v $
  3.  *
  4.  * $Author: Marcel_Offermans $
  5.  *
  6.  * $Revision: 2.2 $
  7.  *
  8.  * $Date: 1994/02/15 21:13:51 $
  9.  *
  10.  * $Locker: Marcel_Offermans $
  11.  *
  12.  * $State: Exp $
  13.  *
  14.  * Description: This is an example of how to use EAGUI. In fact it is the complete version of
  15.  *     the example used in the tutorial[2]. It can be compiled under SAS/C 6.3. It should be
  16.  *     fairly trivial to modify this example to create any window you want[1]. Please note that
  17.  *     the contents of the gadgets aren't saved, so after a resize, everything is lost. Under
  18.  *     V39 it is very easy to get and set these attributes (with GT_GetGadgetAttrs() and
  19.  *     GT_SetGadgetAttrs()), and although it's a bit more difficult under V37, it can be done
  20.  *     there too (it's something you'll have to do anyway).
  21.  *
  22.  * [1] If you want to create a new window, it is enough to specify a new tree of objects. The
  23.  *     only other thing you might want to change is the fact that the window in this example
  24.  *     can only be resized in horizontal direction. If you change the last argument of the
  25.  *     WindowLimits() call to "~0" that's fixed too.
  26.  *
  27.  * [2] In fact, it is a slightly enhanced example, which shows a little bit more.
  28.  *
  29.  * Use a tab size of 5 to read this source! Comments were formatted for a right margin of 95,
  30.  * which matches my overscan and font settings. I hope it is readable for others.
  31.  */
  32.  
  33. /* standard headers */
  34. #include <stdlib.h>
  35.  
  36. #include <exec/types.h>
  37. #include <graphics/text.h>
  38. #include <intuition/intuition.h>
  39. #include <libraries/gadtools.h>
  40.  
  41. #include <clib/macros.h>
  42.  
  43. #include <proto/diskfont.h>
  44. #include <proto/exec.h>
  45. #include <proto/intuition.h>
  46. #include <proto/gadtools.h>
  47. #include <proto/graphics.h>
  48. #include <proto/dos.h>
  49.  
  50. /* EAGUI headers */
  51. #include "EAGUI.h"
  52. #include "EAGUI_pragmas.h"
  53.  
  54. /* Custom object(s) */
  55. #include "TextField.c"
  56.  
  57. /* globals */
  58. struct ea_Object *            winobj_ptr        = NULL;
  59. struct ea_Object *            okobj_ptr            = NULL;
  60. struct ea_Object *            cancelobj_ptr        = NULL;
  61. struct ea_Object *            hgroupobj_ptr        = NULL;
  62. struct Window *            win_ptr            = NULL;
  63. struct Screen *            scr_ptr            = NULL;
  64. struct Gadget *            gadlist_ptr        = NULL;
  65. struct Gadget *            stringgadget_ptr    = NULL;
  66. APTR                     visualinfo_ptr        = NULL;
  67. struct DrawInfo *            drawinfo_ptr        = NULL;
  68. struct TextFont *            tf_ptr            = NULL;
  69. struct Library *            EAGUIBase            = NULL;
  70.  
  71. struct TextAttr             textattr             = {"helvetica.font", 15, FS_NORMAL, FPB_DISKFONT};
  72. struct Hook                relhook;
  73. struct Hook                tfminsizehook;
  74. struct Hook                tfrenderhook;
  75. struct IntuiMessage            imsg;
  76. struct ci_TextField            textfield1;
  77.  
  78. STATIC UBYTE rcs_id_string[] = "$Id: example.c,v 2.2 1994/02/15 21:13:51 Marcel_Offermans Exp Marcel_Offermans $";
  79.  
  80. /* prototypes */
  81. ULONG __saveds __asm HookEntry(register __a0 struct Hook *, register __a2 VOID *, register __a1 VOID *);
  82. VOID InitHook(struct Hook *, ULONG (*)(), VOID *);
  83. ULONG rel_samesize(struct Hook *, struct List *, APTR);
  84. LONG init(VOID);
  85. VOID cleanup(STRPTR);
  86. VOID resizewindow(VOID);
  87. ULONG handlemsgs(VOID);
  88.  
  89. /* functions for hook handling */
  90. ULONG __saveds __asm HookEntry
  91. (
  92.     register __a0 struct Hook *    hook_ptr,
  93.     register __a2 VOID *        object_ptr,
  94.     register __a1 VOID *        message_ptr
  95. )
  96. {
  97.     return((*hook_ptr->h_SubEntry)(hook_ptr, object_ptr, message_ptr));
  98. }
  99.  
  100. VOID InitHook(struct Hook *h_ptr, ULONG (*func_ptr)(), VOID *data_ptr)
  101. {
  102.     if (h_ptr)
  103.     {
  104.         h_ptr->h_Entry = (ULONG (*)())HookEntry;
  105.         h_ptr->h_SubEntry = func_ptr;
  106.         h_ptr->h_Data = data_ptr;
  107.     }
  108. }
  109.  
  110. /* same size relation */
  111. ULONG rel_samesize(struct Hook *hook_ptr, struct List *list_ptr, APTR msg_ptr)
  112. {
  113.      struct ea_RelationObject *ro_ptr;
  114.      ULONG minx, miny;
  115.      ULONG x, y;
  116.      minx = 0;
  117.      miny = 0;
  118.  
  119.      /* examine the list of objects that are affected by the relation */
  120.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  121.      while (ro_ptr->node.ln_Succ)
  122.      {
  123.           ea_GetAttrs(ro_ptr->object_ptr,
  124.                EA_MinWidth,        &x,
  125.                EA_MinHeight,       &y,
  126.                TAG_DONE);
  127.  
  128.           /* find the maximum values of the minimum sizes */
  129.           minx = MAX(x, minx);
  130.           miny = MAX(y, miny);
  131.  
  132.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  133.      }
  134.  
  135.      /* set all objects to the newly found minimum sizes */
  136.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  137.      while (ro_ptr->node.ln_Succ)
  138.      {
  139.           ea_SetAttrs(ro_ptr->object_ptr,
  140.                EA_MinWidth,        minx,
  141.                EA_MinHeight,       miny,
  142.                TAG_DONE);
  143.  
  144.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  145.      }
  146.      return(0);
  147. }
  148.  
  149. /* initialize everything */
  150. LONG init(VOID)
  151. {
  152.     LONG w, h, bl, br, bt, bb;
  153.  
  154.     /* open the EAGUI library */
  155.     if (!(EAGUIBase = OpenLibrary(EAGUILIBRARYNAME, EAGUILIBRARYVERSION)))
  156.     {
  157.         cleanup("Couldn't open EAGUI.library.\n");
  158.     }
  159.  
  160.     /* open the font */
  161.     if (!(tf_ptr = OpenDiskFont(&textattr)))
  162.     {
  163.         cleanup("Couldn't open font.\n");
  164.     }
  165.  
  166.     /* initialize the relation */
  167.     InitHook(&relhook, rel_samesize, NULL);
  168.  
  169.     /* initialize textfield hooks */
  170.     InitHook(&tfminsizehook, meth_MinSize_TextField, NULL);
  171.     InitHook(&tfrenderhook, meth_Render_TextField, NULL);
  172.  
  173.     /* now we can build the object tree */
  174.     if (!( winobj_ptr = VGroup
  175.         EA_BorderLeft,        4,
  176.         EA_BorderRight,    4,
  177.         EA_BorderTop,        4,
  178.         EA_BorderBottom,    4,
  179.         EA_Child,            ea_NewObject(EA_TYPE_CUSTOMIMAGE,
  180.             EA_BorderBottom,    4,
  181.             EA_MinSizeMethod,    &tfminsizehook,
  182.             EA_RenderMethod,    &tfrenderhook,
  183.             EA_UserData,        &textfield1,
  184.             TAG_DONE),
  185.         EA_Child,            GTString(NULL)
  186.             EA_GTTextAttr,        &textattr,
  187.             EA_InstanceAddress,    &stringgadget_ptr,
  188.             EA_MinWidth,        20,    /* Fixes a bug in the GadTools library, which
  189.                                  * renders the full contents of the gadget, if
  190.                                  * it is very small, and you're using a fixed-
  191.                                  * width font. Originally reported by Roy van
  192.                                  * der Woning.
  193.                                  */
  194.             End,
  195.         EA_Child,            hgroupobj_ptr = HGroup
  196.             EA_BorderTop,        4,
  197.             EA_Child,            okobj_ptr = GTButton("Ok")
  198.                 EA_GTTextAttr,        &textattr,
  199.                 End,
  200.             EA_Child,            EmptyBox(1)
  201.                 End,
  202.             EA_Child,            cancelobj_ptr = GTButton("Cancel")
  203.                 EA_GTTextAttr,        &textattr,
  204.                 End,
  205.             End,
  206.         End ) )
  207.     {
  208.         cleanup("Couldn't init the objects.\n");
  209.     }
  210.  
  211.      ea_NewRelation(hgroupobj_ptr, &relhook,
  212.           EA_Object,          okobj_ptr,
  213.           EA_Object,          cancelobj_ptr,
  214.           TAG_DONE);
  215.  
  216.     /* lock the screen */
  217.     if (!(scr_ptr = LockPubScreen(NULL)))
  218.     {
  219.         cleanup("Couldn't lock default public screen.\n");
  220.     }
  221.  
  222.     /* get VisualInfo and DrawInfo */
  223.     if (!(visualinfo_ptr = GetVisualInfo(scr_ptr, TAG_DONE)))
  224.     {
  225.         cleanup("Couldn't get the visual info.\n");
  226.     }
  227.     if (!(drawinfo_ptr = GetScreenDrawInfo(scr_ptr)))
  228.     {
  229.         cleanup("Couldn't get the draw info.\n");
  230.     }
  231.  
  232.     /* fill in the textfield structure */
  233.     textfield1.tf_string_ptr = "Enter a string here:";    /* title */
  234.     textfield1.tf_textattr_ptr = &textattr;                /* font */
  235.     textfield1.tf_flags = CITF_ALIGNTOP;                /* alignment flags */
  236.     textfield1.tf_frontpen = 2;                        /* frontpen color index */
  237.  
  238.      /* obtain the minimum dimensions of every object in the tree */
  239.      ea_GetMinSizes(winobj_ptr);
  240.  
  241.     /* get some attributes */
  242.     ea_GetAttrs(winobj_ptr,
  243.         EA_MinWidth,        &w,
  244.         EA_MinHeight,        &h,
  245.         EA_BorderLeft,        &bl,
  246.         EA_BorderRight,    &br,
  247.         EA_BorderTop,        &bt,
  248.         EA_BorderBottom,    &bb,
  249.         TAG_DONE);
  250.  
  251.      /* open the window */
  252.      if (!(win_ptr = OpenWindowTags(NULL,
  253.          WA_Title,            "EAGUI example",
  254.         WA_Flags,            (WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM | WFLG_ACTIVATE),
  255.         WA_IDCMP,            (IDCMP_CLOSEWINDOW | BUTTONIDCMP | STRINGIDCMP | IDCMP_REFRESHWINDOW | IDCMP_NEWSIZE),
  256.         WA_InnerHeight,    h + bt + bb,
  257.         TAG_DONE)))
  258.     {
  259.         cleanup("Couldn't open the window.\n");
  260.     }
  261.  
  262.     /* set the window limits */
  263.     WindowLimits(
  264.         win_ptr,
  265.         w + win_ptr->BorderLeft + win_ptr->BorderRight + bl + br,
  266.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb,
  267.         ~0,
  268.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb);
  269.  
  270.     /* create the gadgets and add them to the window */
  271.     resizewindow();
  272.  
  273.     return(0);
  274. }
  275.  
  276. /* clean up everything that was created */
  277. VOID cleanup(STRPTR str_ptr)
  278. {
  279.     int rc = 0;
  280.  
  281.     /* if a string is passed, we assume there was some kind of error */
  282.     if (str_ptr)
  283.     {
  284.         Printf("Error: %s", str_ptr);
  285.         rc = 20;
  286.     }
  287.  
  288.     if (gadlist_ptr)
  289.     {
  290.         RemoveGList(win_ptr, gadlist_ptr, -1);
  291.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  292.         gadlist_ptr = NULL;
  293.     }
  294.  
  295.     if (win_ptr)
  296.     {
  297.         CloseWindow(win_ptr);
  298.         win_ptr = NULL;
  299.     }
  300.  
  301.     if (drawinfo_ptr)
  302.     {
  303.         FreeScreenDrawInfo(scr_ptr, drawinfo_ptr);
  304.         drawinfo_ptr = NULL;
  305.     }
  306.  
  307.     if (visualinfo_ptr)
  308.     {
  309.         FreeVisualInfo(visualinfo_ptr);
  310.         visualinfo_ptr = NULL;
  311.     }
  312.  
  313.     if (scr_ptr)
  314.     {
  315.         UnlockPubScreen(NULL, scr_ptr);
  316.         scr_ptr = NULL;
  317.     }
  318.  
  319.     if (winobj_ptr)
  320.     {
  321.         ea_DisposeObject(winobj_ptr);
  322.         winobj_ptr = NULL;
  323.     }
  324.  
  325.     if (tf_ptr)
  326.     {
  327.         CloseFont(tf_ptr);
  328.         tf_ptr = NULL;
  329.     }
  330.  
  331.     if (EAGUIBase)
  332.     {
  333.         CloseLibrary(EAGUIBase);
  334.         EAGUIBase = NULL;
  335.     }
  336.  
  337.     exit(rc);
  338. }
  339.  
  340. /* (re)create the gadget list */
  341. VOID resizewindow(VOID)
  342. {
  343.     LONG bl, br, bt, bb;
  344.  
  345.     /* if necessary, remove the gadget list from the window, and clean it up */
  346.     if (gadlist_ptr)
  347.     {
  348.         RemoveGList(win_ptr, gadlist_ptr, -1);
  349.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  350.         gadlist_ptr = NULL;
  351.     }
  352.  
  353.     ea_GetAttrs(winobj_ptr,
  354.         EA_BorderLeft,        &bl,
  355.         EA_BorderRight,    &br,
  356.         EA_BorderTop,        &bt,
  357.         EA_BorderBottom,    &bb,
  358.         TAG_DONE);
  359.  
  360.      ea_SetAttrs(winobj_ptr,
  361.         EA_Width,        win_ptr->Width -
  362.                     win_ptr->BorderLeft -
  363.                     win_ptr->BorderRight -
  364.                     bl -
  365.                     br,
  366.         EA_Height,    win_ptr->Height -
  367.                     win_ptr->BorderTop -
  368.                     win_ptr->BorderBottom -
  369.                     bt -
  370.                     bb,
  371.         EA_Left,        win_ptr->BorderLeft,
  372.         EA_Top,        win_ptr->BorderTop,
  373.         TAG_DONE);
  374.  
  375.     ea_LayoutObjects(winobj_ptr);
  376.  
  377.     if (ea_CreateGadgetList(winobj_ptr, &gadlist_ptr, visualinfo_ptr, drawinfo_ptr) != EA_ERROR_OK)
  378.     {     
  379.         cleanup("Couldn't create the gadget list.\n");
  380.     }     
  381.  
  382.     EraseRect(win_ptr->RPort,     
  383.         win_ptr->BorderLeft,
  384.         win_ptr->BorderTop,
  385.         win_ptr->Width - win_ptr->BorderRight - 1,
  386.         win_ptr->Height - win_ptr->BorderBottom - 1);
  387.  
  388.     RefreshWindowFrame(win_ptr);
  389.  
  390.     AddGList(win_ptr, gadlist_ptr, -1, -1, NULL);
  391.     RefreshGList(gadlist_ptr, win_ptr, NULL, -1);
  392.     GT_RefreshWindow(win_ptr, NULL);
  393.  
  394.     /* finally, we render the imagery, if there is any */
  395.     ea_RenderObjects(winobj_ptr, win_ptr->RPort);
  396. }
  397.  
  398. /* a normal message handling loop */
  399. ULONG handlemsgs(VOID)
  400. {
  401.     struct IntuiMessage    *    imsg_ptr;
  402.     ULONG                rc = 0;
  403.  
  404.     while (imsg_ptr = GT_GetIMsg(win_ptr->UserPort))
  405.     {
  406.         CopyMem((char *)imsg_ptr, (char *)&imsg, (long)sizeof(struct IntuiMessage));
  407.  
  408.         GT_ReplyIMsg(imsg_ptr);
  409.  
  410.         switch (imsg.Class)
  411.         {
  412.             case    IDCMP_REFRESHWINDOW:
  413.                 GT_BeginRefresh(win_ptr);
  414.                 GT_EndRefresh(win_ptr, TRUE);
  415.                 break;
  416.  
  417.             case    IDCMP_CLOSEWINDOW:
  418.                 rc = 10;
  419.                 break;
  420.  
  421.             case    IDCMP_NEWSIZE:
  422.                 resizewindow();
  423.                 /* Just for fun, we put a string in the string gadget after each
  424.                  * resize. This demonstrates how to use the EA_InstanceAddress
  425.                  * tag to obtain pointers to gadgets, which you can use to modify
  426.                  * the gadgets directly.
  427.                  */
  428.                 GT_SetGadgetAttrs(stringgadget_ptr, win_ptr, NULL,
  429.                     GTST_String,    "Ah, a size change! How nice.",
  430.                     TAG_DONE);
  431.                 break;
  432.         }
  433.     }
  434.     return(rc);
  435. }
  436.  
  437. /* main */
  438. int main(int argc, char *argv[])
  439. {
  440.     ULONG idcmpmask, signals;
  441.     BOOL done = FALSE;
  442.  
  443.     /* process any startup options the user has supplied */
  444.     if (argc > 1)
  445.     {
  446.         /* first argument is the font name */
  447.         textattr.ta_Name = argv[1];
  448.         if (argc > 2)
  449.         {
  450.             /* second argument is the font y-size */
  451.             textattr.ta_YSize = atoi(argv[2]);
  452.         }
  453.     }
  454.  
  455.     /* initialize everything */
  456.     init();
  457.  
  458.     /* event handling loop */
  459.     idcmpmask = 1L << win_ptr->UserPort->mp_SigBit;
  460.     while (done == FALSE)
  461.     {
  462.         signals = Wait(idcmpmask);
  463.         if (signals & idcmpmask)
  464.         {
  465.             if (handlemsgs() != 0)
  466.             {
  467.                 done = TRUE;
  468.             }
  469.         }
  470.     }
  471.  
  472.     /* cleanup everything */
  473.     cleanup(NULL);
  474. }
  475.